/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author JBP
 */
public class WindowsMonitorImpl extends MonitorImpl implements Monitor {

    private Bdd maBdd = new Bdd();

    WindowsMonitorImpl() throws RemoteException {

    }

    /**
     * *************** Memoire *********************************
     */
    @Override
    protected Memoire createMemoire(String ip) throws RemoteException {
        return getMemoryWindows(ip);
    }

    private Memoire getMemoryWindows(String ip) throws RemoteException {
        Memoire m = new Memoire();

        m.setFree(getFreeMemoryWindows());
        m.setTotal(getTotalMemoryWindows());
        m.setUsed(getUsedMemoryWindows(m.getFree(), m.getTotal()));

        maBdd.addMemoryBDD(ip, m.getFree(), m.getUsed(), m.getTotal());

        return m;
    }

    //methode Windows pour connaitre la memoire libre
    public String getFreeMemoryWindows() throws RemoteException {
        ArrayList<String> temp = new ArrayList<String>();
        String free = null;
        try {

            //lancement de la commande
            Process p = Runtime.getRuntime().exec("wmic OS get FreePhysicalMemory /Value");

            //recuperation du resultat dans un buffer
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = null;
            //ajout de chaque ligne du resultat dans une tableau dynamique
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                temp.add(line);
            }

            //recuperation du resultat, on tronque les espaces vides et la verbose
            for (int i = 0; i < temp.size(); i++) {
                if (temp.get(i).length() > 2) {
                    free = temp.get(i).substring(19);
                    System.out.println("Free Memory : " + free);

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return free;

    }

    //methode Windows pour connaitre la memoire totale
    public String getTotalMemoryWindows() throws RemoteException {
        ArrayList<String> temp = new ArrayList<String>();
        String total = null;
        try {

            Process p = Runtime.getRuntime().exec("wmic OS get TotalVisibleMemorySize /Value");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                temp.add(line);
            }

            for (int i = 0; i < temp.size(); i++) {
                if (temp.get(i).length() > 2) {
                    total = temp.get(i).substring(23);
                    System.out.println("Total Memory : " + total);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return total;

    }

    //methode Windows pour connaitre la memoire utilisee
    public String getUsedMemoryWindows(String f, String t) throws RemoteException {
        int temp = 0;
        temp = Integer.parseInt(t) - Integer.parseInt(f);
        String res = String.valueOf(temp);
        return res;

    }

    /**
     * *************** CPU *********************************
     */
    @Override
    protected Cpu createCpu(String ip) throws RemoteException {
        return getCpu(ip);
    }

    //methode accessible au client Linux pour connaitre le pourcentage de CPU utilise
    public Cpu getCpu(String ip) throws RemoteException {
        Cpu moncpu = new Cpu();
        ArrayList<String> temp = new ArrayList<String>();
        String cpu = null;
        try {

            //lancement de la commande
            Process p = Runtime.getRuntime().exec("wmic cpu get LoadPercentage");

            //recuperation du resultat dans un buffer
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = null;
            //ajout de chaque ligne du resultat dans une tableau dynamique
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                temp.add(line);
            }

            //recuperation du resultat, on tronque les espaces vides et la verbose
            for (int i = 0; i < temp.size(); i++) {
                if (temp.get(i).length() > 0) {
                    cpu = temp.get(i).substring(0, 2);
                    System.out.println("CPU : " + cpu);

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        moncpu.setCpu(cpu);

        //ecriture dans la base
        maBdd.addCpuBDD(ip, cpu);

        return moncpu;
    }

    @Override
    protected Processus createProcessus(String ip) throws RemoteException {
        return getProcessus(ip);
    }

    public Processus getProcessus(String ip) throws RemoteException {
        Processus proc = new Processus();

        List<String> retour = new ArrayList<String>();
        try {

            int indexFin = 0;
            Process p = Runtime.getRuntime().exec("TASKLIST /NH /FO \"CSV\"");
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                line = line.replaceFirst("\",\"", " PID: ");
                indexFin = line.indexOf("\",");
                if (0 < indexFin) {
                    System.out.println(indexFin);
                } else {
                    indexFin = line.length();
                }
                retour.add(line.substring(1, indexFin));
            }
        } catch (IOException ex) {

        }

        //ecriture dans la base
        maBdd.addTaskBDD(ip, retour);

        proc.setProcess(retour);

        return proc;
    }

}
